package com.nuanshui.heatedloan.userinfo.service;

import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.framework.cache.CacheConstants;
import com.nuanshui.framework.cache.CacheManager;
import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.heatedloan.userinfo.dal.mapper.UserFansRelationMapper;
import com.nuanshui.heatedloan.userinfo.dal.mapper.UsersMapper;
import com.nuanshui.heatedloan.userinfo.facade.UserFansRelationService;
import com.nuanshui.heatedloan.userinfo.model.UserFansRelation;
import com.nuanshui.heatedloan.userinfo.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("userFansRelationService")
@Transactional(readOnly=true,value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
public class UserFansRelationServiceImpl extends CommonServiceImpl implements UserFansRelationService{

	@Autowired
	private UserFansRelationMapper userFansRelationMapper;

	@Autowired
	private UsersMapper usersMapper;

	@Autowired
	private CacheManager cacheManager;
	
	@Override
	public Long countInterestedByUserId(String userId) {
		return userFansRelationMapper.countInterestedByUserId(userId);
	}

	@Override
	public Long countFansByUserId(String userId) {
		return userFansRelationMapper.countFansByUserId(userId);
	}

	@Override
	public List<UserFansRelation> loadFansByUserId(String userId){
		return userFansRelationMapper.loadFansByUserId(userId);
	}
	
	@Override
	public List<String> loadUserFollowers(String userId){
		List<String> result = new ArrayList<String>();
		if(StringUtils.isEmpty(userId)) return result;
		List<UserFansRelation> list = userFansRelationMapper.loadUserFollowers(userId);
		
		if(list!=null){
			for (int i = 0; i < list.size(); i++) {
				result.add(list.get(i).getTargetUserId());
			}
		}
		return result;
	}
	
	@Override
	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public void insert(String sourceUserId, String targetUserId) {
		userFansRelationMapper.insert(sourceUserId, targetUserId);
		cacheManager.evictCache(CacheConstants.MEG_LIST, sourceUserId);
		cacheManager.evictCache(CacheConstants.USER_RELATION_FOLLOWS,sourceUserId);
	}

	@Override
	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public void delete(String sourceUserId, String targetUserId) {
		userFansRelationMapper.delete(sourceUserId, targetUserId);
		cacheManager.evictCache(CacheConstants.MEG_LIST, sourceUserId);
		cacheManager.evictCache(CacheConstants.USER_RELATION_FOLLOWS,sourceUserId);
	}

	@Override
	public Boolean isFans(String sourceUserId, String targetUserId) {
		return userFansRelationMapper.isFans(sourceUserId, targetUserId)==1;
	}

	/**
	 * 获取关注的用户
	 * @param 
	 * @author guohao
	 * @date 2015年4月22日 上午10:47:13
	 */
	@Override
	public List<UserVo> findInterestedUser(String userId,String loginedId,Pageable pageable) {
			return userFansRelationMapper.findInterestedUser(userId, loginedId,pageable);
	}

	/**
	 * 获取关注的粉丝用户
	 * @param 
	 * @author guohao
	 * @date 2015年4月22日 上午10:47:13
	 */
	@Override
	public List<UserVo> findAllUserFans(String userId,String loginedId) {
			return userFansRelationMapper.findAllUserFans(userId, loginedId);
	}
	
	@Override
	public List<UserVo> findUserFans(String userId,String loginedId,Pageable pageable) {
		return userFansRelationMapper.findUserFans(userId, loginedId, pageable);
	}

	@Override
	public List<String> loadFansIdsByUserId(String userId) {
		return userFansRelationMapper.loadFansIdsByUserId(userId);
	}

	@Override
	public List<HashMap<String, Long>> findUserFansCount() {

		return usersMapper.findUserFansCount();
	}

}
